import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'item/home_item_page1.dart';

class MainForumPage extends StatefulWidget {
  @override
  _MainForumPageState createState() => _MainForumPageState();
}

class _MainForumPageState extends State<MainForumPage>
    with SingleTickerProviderStateMixin {
  List<String> topTabList = [
    "推荐",
    "动态",
    "Java",
    "程序人生",
    "移动开发",
    "数据算法",
    "程序感言"
  ];
  List<String> imageList = [
    "https://cdn.pixabay.com/photo/2017/07/10/23/45/cubes-2492010_960_720.jpg",
    "https://cdn.pixabay.com/photo/2018/06/07/16/49/vr-3460451_960_720.jpg",
    "https://cdn.pixabay.com/photo/2015/11/09/14/43/laptop-1035345_960_720.jpg"
  ];
  int bannerIndex = 0;
  List<Widget> bodyPageList = [];
  TabController tabController;
  @override
  void initState() {
    super.initState();
    tabController = new TabController(length: topTabList.length, vsync: this);
    for (int i = 0; i < topTabList.length; i++) {
      bodyPageList.add(new HomeItemPage1(i, topTabList[i]));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RefreshIndicator(
        notificationPredicate: (notifation) {
          // 返回true即可
          return true;
        },
        onRefresh: onRefresh,
        child: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool b) {
            return [
              SliverAppBar(
                // 设置SliverAppBar 是否固定
                pinned: true,
                // 是否随着滑动来隐藏标题
                floating: true,
                actions: [
                  IconButton(
                      icon: Icon(Icons.add_circle_rounded),
                      onPressed: () {
                        print("点击了按钮");
                      }),
                ],
                expandedHeight: 240,
                flexibleSpace: FlexibleSpaceBar(
                  background: buildBannerWidget(),
                ),
                bottom: TabBar(
                  // 是否可滑动
                  isScrollable: true,
                  controller: tabController,
                  tabs: topTabList
                      .map((String tabTitle) => Tab(text: tabTitle))
                      .toList(),
                ),
              )
            ];
          },
          body: TabBarView(
            controller: tabController,
            children: bodyPageList,
          ),
        ),
      ),
    );
  }

  Future<void> onRefresh() async {
    print("开始下拉刷新");
    await Future.delayed(Duration(milliseconds: 3000), () {
      print("结束下拉刷新");
    });
  }

  Widget buildBannerWidget() {
    return Container(
      height: 200,
      child: Stack(
        children: [
          Positioned(
            top: 0,
            bottom: 0,
            left: 0,
            right: 0,
            child: Swiper(
              onIndexChanged: (value) {
                setState(() {
                  bannerIndex = value;
                });
              },
              itemCount: imageList.length,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  height: 200,
                  child: Stack(
                    children: [
                      Positioned(
                        child:
                            Image.network(imageList[index], fit: BoxFit.fill),
                        left: 0,
                        right: 0,
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
          Positioned(
            bottom: 10,
            right: 10,
            child: Container(
              padding: EdgeInsets.only(
                left: 5,
                right: 5,
                top: 2,
                bottom: 2,
              ),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(10),
                  )),
              child: Text(
                "${bannerIndex + 1}/${imageList.length}",
                style: TextStyle(fontSize: 10),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
